import pymysql

MY_SQL = {
    "host": "192.168.2.100",
    "user": 'mysql账号',
    "password": 'mysql密码',
    "db": 'qywx',
}


def mysqlUpdata(data: list):
    global MY_SQL
    my_sql = pymysql.connect(**MY_SQL)
    cursor = my_sql.cursor()
    try:
        mysql = rf"INSERT INTO wxtask(webhookurl, imagepath, message, resultDate,job_id, createtime) VALUES (%s, %s, %s, %s, %s, %s)"
        cursor.execute(mysql, data)
        my_sql.commit()
        print('执行完成')
        my_sql.close()
        return 1
    except:
        return 2



def SelectaskList(webhookurl:str):
    global MY_SQL
    my_sql = pymysql.connect(**MY_SQL)
    cursor = my_sql.cursor()
    # 执行查询
    try:
        mysqlle = f"SELECT * FROM wxtask where webhookurl='{webhookurl}' and del_flag=1 ORDER BY id DESC "
        cursor.execute(mysqlle)
        res = cursor.fetchall()
        my_sql.close()
        return res
    except:
        return  2

# 设置 执行完成 修改状态
def updataMysql(message:str):
    global MY_SQL
    my_sql = pymysql.connect(**MY_SQL)
    cursor = my_sql.cursor()
    # 执行查询
    try:
        mysqlle = f"UPDATE wxtask set status=1 where message='{message}' and del_flag=1 "
        cursor.execute(mysqlle)
        my_sql.commit()
        print('数据更新完成')
        my_sql.close()
        return 1
    except:
        return 2


# 取消任务
def updataMysqlcalsh(job_id:str):
    global MY_SQL
    my_sql = pymysql.connect(**MY_SQL)
    cursor = my_sql.cursor()
    # 执行查询
    try:
        mysqlle = f"UPDATE wxtask set status=3 where job_id='{job_id}' and del_flag=1 "
        cursor.execute(mysqlle)
        my_sql.commit()
        print('数据更新完成')
        my_sql.close()
        return 1
    except:
        return 2
